package com.example.pages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import static org.junit.Assert.*;

public class ProfilePage extends InternalPage{

    @FindBy(name = "password")
    public WebElement passwordChange;

    @FindBy(name = "password2")
    public WebElement passwordChangeRepeat;

    @FindBy (name = "submit")
    public WebElement saveChange;

    @FindBy(className = "error")
    public WebElement error;

    @FindBy(xpath = "//h2[text() = 'Edit admin']")
    public WebElement titlePageProfile;

    public boolean isOnThisPage(){
        return titlePageProfile.isDisplayed();
    }

    @Override
    void tryToOpen(){
        MyPageFactory.getPage(driver, LoginPage.class)
            .loginWithValidCredentials("admin", "admin")
            .gotoUserManagementConsolPage();
    }

    public ProfilePage editFormIncorrectPassword(String password) {
        passwordChange.sendKeys(password);
        passwordChangeRepeat.sendKeys(password + 1);
        saveChange.click();
        assertTrue(isOnProfilePageError());
        return MyPageFactory.getPage(driver, ProfilePage.class);
    }

    public UserPage editCorrectPassword(String password) {
        passwordChangeRepeat.sendKeys(password);
        saveChange.click();
        return MyPageFactory.getPage(driver, UserPage.class);
    }

    public UserPage setDefaultValues() {
        passwordChange.clear();
        passwordChange.sendKeys("admin");
        passwordChangeRepeat.clear();
        passwordChangeRepeat.sendKeys("admin");
        saveChange.click();
        return MyPageFactory.getPage(driver, UserPage.class);
    }

    public boolean isOnProfilePageError() {
        return error.isDisplayed();
    }
}
